package com.haixiaoke.saas.repairs.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.constant.HouseConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.house.domain.House;
import com.haixiaoke.saas.house.service.IHouseService;
import com.haixiaoke.saas.repairs.domain.Repairs;
import com.haixiaoke.saas.repairs.mapper.RepairsMapper;
import com.haixiaoke.saas.repairs.service.IRepairsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 报修Service业务层处理
 *
 * @author panda
 * @date 2025-07-05
 */
@Service
public class RepairsServiceImpl implements IRepairsService {
    @Autowired
    private RepairsMapper repairsMapper;
    @Autowired
    private IHouseService houseService;

    /**
     * 查询报修
     *
     * @param id 报修主键
     * @return 报修
     */
    @Override
    public Repairs selectRepairsByRepairsId(Long id) {
        return repairsMapper.selectRepairsByRepairsId(id);
    }

    /**
     * 查询报修列表
     *
     * @param repairs 报修
     * @return 报修
     */
    @Override
    public List<Repairs> selectRepairsList(Repairs repairs) {
        return repairsMapper.selectRepairsList(repairs);
    }

    /**
     * 新增报修
     *
     * @param repairs 报修
     * @return 结果
     */
    @Override
    public AjaxResult insertRepairs(Repairs repairs) {
        if (StrUtil.isBlank(repairs.getContactName()) || StrUtil.isBlank(repairs.getContactPhone())) {
            return AjaxResult.error("联系人姓名或联系人手机号不能为空");
        }
        if (StrUtil.isBlank(repairs.getRepairsMessage())) {
            return AjaxResult.error("报修内容不能为空");
        }
        if (repairs.getConUserId() == null) {
            return AjaxResult.error("报修人信息错误");
        }
        House house = houseService.selectHouseByHouseId(repairs.getHouseId());
        if (ObjectUtil.isEmpty(house)) {
            return AjaxResult.error("房源不存在");
        }
        repairs.setRepairsState(HouseConstants.REPAIRS_STATE_WAITING);
        repairs.setCreateTime(DateUtils.getNowDate());
        repairs.setTenantId(house.getTenantId());
        int result = repairsMapper.insertRepairs(repairs);
        return result > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改报修
     *
     * @param repairs 报修
     * @return 结果
     */
    @Override
    public int updateRepairs(Repairs repairs) {
        repairs.setUpdateTime(DateUtils.getNowDate());
        return repairsMapper.updateRepairs(repairs);
    }

    @Override
    public int softDeleteRepairsByRepairsId(Long repairsId, String tenantId) {
        Repairs repairs = repairsMapper.selectRepairsByRepairsId(repairsId);
        if (!StrUtil.equals(tenantId, repairs.getTenantId())) {
            throw new ServiceException("不能操作其他机构的报修");
        }
        return repairsMapper.softDeleteRepairsByRepairsId(repairsId);
    }

    @Override
    public AjaxResult completeRepairs(Long repairsId, String userName, String tenantId) {
        Repairs repairs = repairsMapper.selectRepairsByRepairsId(repairsId);
        if (ObjectUtil.isEmpty(repairs)) {
            return AjaxResult.error("报修不存在");
        }
        if (!StrUtil.equals(tenantId, repairs.getTenantId())) {
            return AjaxResult.error("不能操作其他机构的报修");
        }
        if (!StrUtil.equals(HouseConstants.REPAIRS_STATE_WAITING, repairs.getRepairsState())) {
            return AjaxResult.error("报修状态不正确");
        }
        repairs.setRepairsState(HouseConstants.REPAIRS_STATE_COMPLETE);
        repairs.setUpdateTime(DateUtils.getNowDate());
        repairs.setUpdateBy(userName);
        int result = repairsMapper.updateRepairs(repairs);
        return result > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public AjaxResult checkRepairs(Long repairsId, String userName) {
        Repairs repairs = repairsMapper.selectRepairsByRepairsId(repairsId);
        if (ObjectUtil.isEmpty(repairs)) {
            return AjaxResult.error("报修不存在");
        }
        if (!StrUtil.equals(HouseConstants.REPAIRS_STATE_COMPLETE, repairs.getRepairsState())) {
            return AjaxResult.error("报修状态不正确");
        }
        repairs.setRepairsState(HouseConstants.REPAIRS_STATE_APPROVED);
        repairs.setUpdateTime(DateUtils.getNowDate());
        repairs.setUpdateBy(userName);
        int result = repairsMapper.updateRepairs(repairs);
        return result > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public int countWaiting(String tenantId) {
        return repairsMapper.countWaiting(tenantId);
    }

    /**
     * 批量删除报修
     *
     * @param ids 需要删除的报修主键
     * @return 结果
     */
    @Override
    public int deleteRepairsByRepairsIds(Long[] ids) {
        return repairsMapper.deleteRepairsByRepairsIds(ids);
    }

    /**
     * 删除报修信息
     *
     * @param id 报修主键
     * @return 结果
     */
    @Override
    public int deleteRepairsByRepairsId(Long id) {
        return repairsMapper.deleteRepairsByRepairsId(id);
    }
}
